package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.VocCategoryConverter;
import com.cskaoyan.wordmemorize.converter.VocConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocCategoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookCategoryOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import com.cskaoyan.wordmemorize.service.VocChapterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class VocCategoryServiceImpl extends ServiceImpl<VocCategoryMapper, VocCategoryDO> implements VocCategoryService {

    @Resource
    VocCategoryMapper vocCategoryMapper;

    @Resource
    VocCategoryConverter vocCategoryConverter;
    @Override
    public PageDTO<VocCategoryDTO> getBookCategories(PageRequest pageRequest) {
        //判断分页参数是否存在
          //不存在则进行全表查询
        if (pageRequest.getPageNum() == null || pageRequest.getCount() == null) {
            return vocCategoryConverter.bookCategoryList2Page(vocCategoryMapper.selectList(null), 1L);
        }
        Page<VocCategoryDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());

        LambdaQueryWrapper<VocCategoryDO> vocCategoryDOLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (pageRequest.getKeyword() != null) {
            vocCategoryDOLambdaQueryWrapper.like(VocCategoryDO::getCategoryName, pageRequest.getKeyword());
        }
        Page<VocCategoryDO> result = vocCategoryMapper.selectPage(page, vocCategoryDOLambdaQueryWrapper);
        PageDTO<VocCategoryDTO> pageDTO = vocCategoryConverter.bookCategoryList2Page(result.getRecords(), page.getTotal());
        return pageDTO;
    }

    @Override
    public VocCategoryDTO getBookCategory(Long categoryId) {
        if (categoryId != null) {
            VocCategoryDO bookCategoryDO = vocCategoryMapper.selectById(categoryId);
            if (bookCategoryDO != null) {
                return vocCategoryConverter.bookCategoryEntity2DTO(bookCategoryDO);
            }
        }
        return null;
    }

    @Override
    public Long addBookCategory(VocCategoryCommand command) {
        VocCategoryDO bookCategoryDO = vocCategoryConverter.bookCategoryCommand2Entity(command);
        int insert = vocCategoryMapper.insert(bookCategoryDO);
        if (insert > 0) {
            return bookCategoryDO.getId();
        }
        return null;
    }

    @Override
    public void updateBookCategory(VocCategoryCommand command) {
        VocCategoryDO bookCategoryDO = vocCategoryConverter.bookCategoryCommand2Entity(command);
        vocCategoryMapper.updateById(bookCategoryDO);
    }

    @Override
    public DeleteBookCategoryOperateDTO deleteBookCategory(Long categoryId) {
        DeleteBookCategoryOperateDTO deleteBookCategoryOperateDTO = vocCategoryConverter.bookCategoryDOToDeleteCategoryDTO(vocCategoryMapper.selectById(categoryId));
        vocCategoryMapper.deleteById(categoryId);
        return null;
    }

    @Override
    public List<DeleteBookCategoryOperateDTO> deleteBookCategories(BatchDeleteCommand command) {
        List<DeleteBookCategoryOperateDTO> deleteBookCategoryOperateDTOS = vocCategoryConverter.bookCategoryDOsToDeleteCategoryDTOs(vocCategoryMapper.selectBatchIds(command.getIds()));
        vocCategoryMapper.deleteBatchIds(command.getIds());
        return deleteBookCategoryOperateDTOS;
    }
}
